package com.c2soft.nepalcmx.dao;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import com.c2soft.nepalcmx.command.OrderCommand;
import com.c2soft.nepalcmx.command.WithdrawCommand;

public class WithdrawlDAO {

	@Autowired
	private DataSource dataSource;

	private JdbcTemplate jdbcTemplate;
	
	public void saveWithdrawService(WithdrawCommand withdrawl) {
	String sql = "INSERT INTO withdrawal (brokeId,USER_ID,FNAME,EMAILID,IFSC,ACCNO,BNAME,AMOUNT,w_MODE,COMMENTS) VALUES (?,?,?,?,?,?, ?,?, ?,?)";
			
			jdbcTemplate = new JdbcTemplate(dataSource);
			jdbcTemplate.update(sql,
					new Object[] {withdrawl.getBrokeId(), withdrawl.getUser_Id(),withdrawl.getFname(),withdrawl.getEmailid(),withdrawl.getIfsc(),withdrawl.getAccno(),
					withdrawl.getBname(),withdrawl.getAmount(),withdrawl.getW_mode(),withdrawl.getComments()
					});
	}
	
	
	public List<WithdrawCommand> findAllWithdrawals(String brokerId) {
		String sql = "select * from withdrawal where user_id in(select user_id from brokers where broker_id='"+brokerId+"' and user_id!='')";
		jdbcTemplate = new JdbcTemplate(dataSource);
		List<WithdrawCommand> withdrawal=jdbcTemplate.query(sql,new BeanPropertyRowMapper(WithdrawCommand.class));
		
		return withdrawal;
	}
	
	public List<WithdrawCommand> findAllBrokersWithdrawals() {
		String sql = "select * from withdrawal where brokeId!=''";
		jdbcTemplate = new JdbcTemplate(dataSource);
		List<WithdrawCommand> withdrawal=jdbcTemplate.query(sql,new BeanPropertyRowMapper(WithdrawCommand.class));
		
		return withdrawal;
	}

	public void deleteWithdrawal(String id){
		jdbcTemplate = new JdbcTemplate(dataSource);
		String sql="Delete from withdrawal where id=?";
		jdbcTemplate.update(sql,new Object[]{id});
		
		    }
	
	
	
}
